//
//  ReminderModalDetailView.m
//  Reminders
//
//  Created by Nearsoft User on 8/22/12.
//
//

#import "ReminderModalDetailView.h"

@interface ReminderModalDetailView ()

@end

@implementation ReminderModalDetailView

@synthesize reminderPk;
@synthesize delegate;

#pragma mark - Default Methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setViewElements];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"modalViewBackground.png"]]];
}

- (void)viewDidUnload
{
    notes = nil;
    aReminder = nil;
    connect = nil;
    dateString = nil;

    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Reminder Methods
/**
 * Method name: getReminderData
 * Description: Gets the remainder data to be displayed on the view
 * Parameters:
 */
- (BOOL)getReminderData
{
    if (connect == nil)
        connect = [[Connection alloc] init];

    aReminder = [connect getSingleReminder:reminderPk];

    if (aReminder == nil)
        return NO;

    reminderRepeat = [aReminder repeat];

    return YES;
}

- (void)setViewElements
{
    if (dateString == nil)
        dateString = [[DateStrings alloc] init];

    if (aReminder == nil)
        [self getReminderData];

    remTitle.text = [aReminder title];
    date.text = [dateString fullStringFromDate:[aReminder date]];
    notes.text = [aReminder notes];

    switch (reminderRepeat)
    {
                // Every Day
        case 1: repeat.text = [NSString stringWithFormat:@"Every day  at %@", [dateString stringFromTime:aReminder.date]];
                break;
                // Every Week on the same Day
        case 2: repeat.text = [NSString stringWithFormat:@"Every week at %@", [dateString stringFromTime:aReminder.date]];
                break;
                // Every Month on the same Day
        case 3: repeat.text = [NSString stringWithFormat:@"Every month at %@", [dateString stringFromTime:aReminder.date]];
                break;
                //Just Once
        default: repeat.text = @"This is a one time event";
                break;
    }
}

/**
 * Method name: updateReminder
 * Description: Updates the reminder from the database
 * Parameters: pk: The reminder that will be updated
 */
- (void)updateReminder;
{
    if (connect == nil)
        connect = [[Connection alloc] init];

    if (aReminder == nil)
        [self getReminderData];

    if (reminderRepeat == 0)
        [connect updateReminderCompleteFromDatabase:reminderPk];
    else
    {
        if (dateString == nil)
            dateString = [[DateStrings alloc] init];

        if ([dateString dateIsOlderThanToday:aReminder.date])
            [connect updateReminderDateFromDatabase:reminderPk];
    }
}

/**
 * Method name: popView
 * Description: Update the reminder, dissmiss the view and change the alertShown flag
 * Parameters: sender: not used at the moment
 */
- (IBAction)popView:(id)sender
{
    [self updateReminder];
    [self dismissModalViewControllerAnimated:YES];
    [self.delegate passAlertShown];
}

@end
